
/**
 * 兼容流域支持系统一期
 */
import { RouteBreadcrumbMapping, VmsMenu } from '/@/_types/sys/menu';

/**
 * 路由组件id映射 
 * 这个是一个历史遗留问题 报表报告 实时计算他们的组件id是一样的 我们无法通过url获取到组件 无法选中一级菜单 所以只得有单独再做一个映射 url=>第一级菜单的id
 */
export function routecompIdMapping(vmsMenus: Array<VmsMenu>): Map<string, string> {
    const mappingMap: Map<string, string> = new Map();
    vmsMenus.forEach(vmsMenu => {
        if (vmsMenu.children && vmsMenu.children.length > 0) {
            _routeCompIdMapping(vmsMenu.children, mappingMap, vmsMenu.id)
        }
    })
    return mappingMap;
}

function _routeCompIdMapping(vmsMenus: Array<VmsMenu>, mappingMap: Map<string, string>, val: string) {
    vmsMenus.forEach(vmsMenum => {
        if (vmsMenum.attributes.url) {
            mappingMap.set(vmsMenum.attributes.url, val)
        }
        if (vmsMenum.children && vmsMenum.children.length > 0) {
            _routeCompIdMapping(vmsMenum.children, mappingMap, val);
        }
    })
}

/**
 * 路由 菜单标题映射
 * @param vmsMenus 
 * @returns 
 */
export function routeBreadcrumbMapping(vmsMenus: Array<VmsMenu>): Map<string, RouteBreadcrumbMapping> {
    const mappingMap: Map<string, RouteBreadcrumbMapping> = new Map();
    vmsMenus.forEach(vmsMenu => {
        const routeBreadcrumbMapping = { id: vmsMenu.id, url: vmsMenu.attributes.url || vmsMenu.attributes.vuePath, title: vmsMenu.text, parent: null }
        if (vmsMenu.attributes.url) {
            mappingMap.set(vmsMenu.attributes.url, routeBreadcrumbMapping)
        }
        if (vmsMenu.children && vmsMenu.children.length > 0) {
            _routeBreadcrumbMapping(vmsMenu.children, routeBreadcrumbMapping, mappingMap)
        }
    })
    return mappingMap;
}

function _routeBreadcrumbMapping(vmsMenus: Array<VmsMenu>, routeBreadcrumbMapping: RouteBreadcrumbMapping, mappingMap: Map<string, RouteBreadcrumbMapping>) {
    vmsMenus.forEach(vmsMenu => {
        const routeBreadcrumbMappingChild = { id: vmsMenu.id, url: vmsMenu.attributes.url || vmsMenu.attributes.vuePath, title: vmsMenu.text, parent: routeBreadcrumbMapping }
        if (vmsMenu.attributes.url) {
            mappingMap.set(vmsMenu.attributes.url, routeBreadcrumbMappingChild)
        }
        if (vmsMenu.children && vmsMenu.children.length > 0) {
            _routeBreadcrumbMapping(vmsMenu.children, routeBreadcrumbMappingChild, mappingMap);
        }
    })
}